package io.hilo.example.demo.client;

import org.jretty.apibase.Request;
import org.jretty.apibase.Result;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import com.alibaba.dubbo.config.annotation.Reference;

import org.hilo.boot.core.UT;
import io.hilo.example.account.api.SimpleService;
import io.hilo.example.account.dto.simple.AddNumberReq;

/**
 * 
 * @author zollty
 * @since 2018年2月24日
 */
@Component
public class TestConsumer implements CommandLineRunner {
    
    // 使用dubbo原生注解@Reference注入
    @Reference 
    SimpleService service; 
    
    void test01(int n) {
        while (n-- > 0) {
            int a = -1;
            int b = 10;

            UT.Thread.sleepThread(500);
            AddNumberReq req = new AddNumberReq();
            req.setA(a);
            req.setB(b);
            Result<Integer> ret = service.addNumber(Request.data(req));
            if (ret.isSuccess()) {
                System.err.printf("%s+%s=%s", a, b, ret.getData());
                System.err.println();
            } else {
                System.err.printf(ret.getCode() + ": " + ret.getMsg());
                System.err.println();
            }

        }
    }
    
    void test02(int a, int b) {
        AddNumberReq req = new AddNumberReq();
        req.setA(a);
        req.setB(b);
        Result<Integer> ret = service.addNumber(Request.data(req));
        if (ret.isSuccess()) {
            System.err.printf("%s+%s=%s", a, b, ret.getData());
            System.err.println();
        } else {
            System.err.printf(ret.getCode() + ": " + ret.getMsg());
            System.err.println();
        }
        
    }
    
    @Override
    public void run(String... args) {
        try {
            test();
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 测试完主动退出
        System.exit(0);
    }
    
    void test() {
        test01(10);
        System.out.println("--------1---------");
        test02(10, 18);
        System.out.println("--------2---------");
        test02(10, 0);
        System.out.println("--------3---------");
        test02(0, 18);
        System.out.println("--------4---------");
        test02(2, -10);
        System.out.println("--------3---------");
        test02(-10, 0);
    }

}
